/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadCompra.java
 *
 * Created on 22/11/2011, 20:43:12
 */
package visao;

import compra.Compra;
import descricaoProduto.DescricaoProduto;
import fachada.FachadaBDR;
import fornecedor.Fornecedor;
import fornecedor.FornecedorNaoCadastradoException;
import funcionario.Funcionario;
import java.awt.Color;
import java.sql.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import solicitacaoCompra.SolicitacaoCompra;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;
import util.KeyValue;
import util.Util;

/**
 *
 * @author Administrador
 */
public class CadCompra extends javax.swing.JFrame {

    /** Creates new form CadCompra */
    public CadCompra(int idCompra) {
        initComponents();
        this.setLocationRelativeTo(null);

        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/Compra.gif")).getImage());

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            Compra objCompra = objFachadaBDR.procurarCompra(idCompra);

            SolicitacaoCompra objSolicitacaoCompra = objCompra.getSolicitacaoCompra();
            Fornecedor objForn = objCompra.getFornecedor();
            Funcionario objFuncionario = objCompra.getFuncionario();

            jTextField_idCodigo.setText(String.valueOf(objCompra.getIdCompra()));
            jComboBox_idFornecedor.setSelectedItem(new KeyValue(objForn.getDescricaoFornecedor(), Integer.toString(objForn.getIdFornecedor())));
            jComboBox_idFuncionario.setSelectedItem(new KeyValue(objFuncionario.getNome(), Integer.toString(objFuncionario.getIdFuncionario())));
            jComboBox_idSolicitacaoCompra.setSelectedItem(new KeyValue(objSolicitacaoCompra.getDescricaoProduto().getDecsricaoProduto(), Integer.toString(objSolicitacaoCompra.getIdSolicitacaoCompra())));
            jComboBox_idSolicitacaoCompra.setEnabled(false);
            jTextField_Quantidade.setText(String.valueOf(objCompra.getQtdProduto()));
            jFormattedTextField_DataCompra.setText(Util.formatarBancoData(objCompra.getDataCompra().toString()));
            jCheckBox_status.setSelected(objCompra.getStatusCompra());
            String valorCompra = objCompra.getValorCompra().toString();
            valorCompra.replace(".", ",");

            jFormattedTextField_ValorCompra.setText(valorCompra);

        } catch (Exception ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        }

    }

    /** Creates new form CadCompra */
    public CadCompra(int idCompra, int idSolicitacaoCompra) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/Compra.gif")).getImage());
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            SolicitacaoCompra objSolicitacaoCompra = objFachadaBDR.procurarSolicitacaoCompra(idSolicitacaoCompra);

            jComboBox_idSolicitacaoCompra.setSelectedItem(new KeyValue(objSolicitacaoCompra.getDescricaoProduto().getDecsricaoProduto(), Integer.toString(objSolicitacaoCompra.getIdSolicitacaoCompra())));
            jComboBox_idSolicitacaoCompra.setEnabled(false);
            jCheckBox_status.setSelected(true);

        } catch (Exception ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel_Codigo = new javax.swing.JLabel();
        jLabel_idFornecedor = new javax.swing.JLabel();
        jLabel_idSolicitacaoCompra = new javax.swing.JLabel();
        jLabel_idFuncionario = new javax.swing.JLabel();
        jLabel_valorCompra = new javax.swing.JLabel();
        jLabel_Quantidade = new javax.swing.JLabel();
        jComboBox_idFornecedor = new javax.swing.JComboBox();
        jComboBox_idSolicitacaoCompra = new javax.swing.JComboBox();
        jComboBox_idFuncionario = new javax.swing.JComboBox();
        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jLabel_Mensagem = new javax.swing.JLabel();
        jFormattedTextField_ValorCompra = new javax.swing.JFormattedTextField();
        jTextField_Quantidade = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jFormattedTextField_DataCompra = new javax.swing.JFormattedTextField();
        jCheckBox_status = new javax.swing.JCheckBox();
        jTextField_idCodigo = new javax.swing.JTextField();

        setTitle("Compra");
        setResizable(false);

        jLabel_Codigo.setText("Codigo :");

        jLabel_idFornecedor.setText("Fornecedor :");

        jLabel_idSolicitacaoCompra.setText("Solicitação Compra :");

        jLabel_idFuncionario.setText("Funcionario :");

        jLabel_valorCompra.setText("Valor Compra :");

        jLabel_Quantidade.setText("Quantidade :");

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            List<Fornecedor> listFornecedor = objFachadaBDR.listarFornecedor("");
            jComboBox_idFornecedor.addItem(new KeyValue("--- Favor Selecionar ---", "0"));
            for (Fornecedor objForn : listFornecedor) {
                jComboBox_idFornecedor.addItem(new KeyValue(objForn.getDescricaoFornecedor(), Integer.toString(objForn.getIdFornecedor())));
            }
        } catch (Exception e) {
        }
        jComboBox_idFornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_idFornecedorActionPerformed(evt);
            }
        });

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            List<SolicitacaoCompra> listSolicitacaoCompra = objFachadaBDR.listarSolicitacaoCompra("");
            jComboBox_idSolicitacaoCompra.addItem(new KeyValue("--- Favor Selecionar ---", "0"));
            for (SolicitacaoCompra objSolicitacaoCompra : listSolicitacaoCompra) {
                jComboBox_idSolicitacaoCompra.addItem(new KeyValue(objSolicitacaoCompra.getDescricaoProduto().getDecsricaoProduto(), Integer.toString(objSolicitacaoCompra.getIdSolicitacaoCompra())));
            }
        } catch (Exception e) {
        }

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            List<Funcionario> listFuncionario = objFachadaBDR.listarFuncionario("");

            jComboBox_idFuncionario.addItem(new KeyValue("--- Favor Selecionar ---","0"));
            for (Funcionario objFuncionario : listFuncionario) {
                jComboBox_idFuncionario.addItem(new KeyValue(objFuncionario.getNome(), Integer.toString(objFuncionario.getIdFuncionario())));
            }

        } catch (Exception e) {
        }

        jButton_Ok.setText("Ok");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        jFormattedTextField_ValorCompra.setText("00000.00");

        jTextField_Quantidade.setText("0");

        jLabel1.setText("Data Compra :");

        jFormattedTextField_DataCompra = new JFormattedTextField(Util.getDataAtual());

        jCheckBox_status.setSelected(true);
        jCheckBox_status.setText("Ativo");

        jTextField_idCodigo.setText("0");
        jTextField_idCodigo.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_idFuncionario, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel_idFornecedor, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel_idSolicitacaoCompra, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel_Codigo, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jComboBox_idFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox_idFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox_idSolicitacaoCompra, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextField_idCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jCheckBox_status)
                                .addGap(79, 79, 79))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(216, 216, 216)
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(216, 216, 216)
                                .addComponent(jLabel_valorCompra))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jButton_Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35)
                                .addComponent(jLabel_Quantidade)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jFormattedTextField_DataCompra, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jFormattedTextField_ValorCompra, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField_Quantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 492, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Codigo)
                            .addComponent(jTextField_idCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jCheckBox_status)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel_idFornecedor))
                    .addComponent(jComboBox_idFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel_idSolicitacaoCompra))
                    .addComponent(jComboBox_idSolicitacaoCompra, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel_idFuncionario))
                    .addComponent(jComboBox_idFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel1))
                            .addComponent(jFormattedTextField_DataCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel_valorCompra))
                            .addComponent(jFormattedTextField_ValorCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel_Quantidade))
                            .addComponent(jTextField_Quantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(23, 23, 23))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton_Ok)
                            .addComponent(jButton_Cancelar))
                        .addGap(4, 4, 4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:

        int idCompra = Integer.parseInt(jTextField_idCodigo.getText());

        int idFuncionario = Integer.parseInt(((KeyValue) jComboBox_idFuncionario.getSelectedItem()).getValue());
        int idFornecedor = Integer.parseInt(((KeyValue) jComboBox_idFornecedor.getSelectedItem()).getValue());
        int idSolicitacaoCompra = Integer.parseInt(((KeyValue) jComboBox_idSolicitacaoCompra.getSelectedItem()).getValue());
        int quantidade = Integer.parseInt(jTextField_Quantidade.getText());
        double valor = Double.parseDouble(jFormattedTextField_ValorCompra.getText());
        Date dataCompra = Date.valueOf(Util.formatarDataBanco(jFormattedTextField_DataCompra.getText()));
        boolean status = jCheckBox_status.isSelected();

        try {

            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            Funcionario funcionario = objFachadaBDR.procurarFuncionario(idFuncionario);
            Fornecedor fornecedor = objFachadaBDR.procurarFornecedor(idFornecedor);
            SolicitacaoCompra solicitacaoCompra = objFachadaBDR.procurarSolicitacaoCompra(idSolicitacaoCompra);

            Compra compra = new Compra(idCompra, solicitacaoCompra, funcionario, fornecedor, valor, dataCompra, quantidade, status);

            if (idCompra == 0) {
                objFachadaBDR.cadastrarCompra(compra);

                jComboBox_idFornecedor.setSelectedIndex(0);
                jComboBox_idFuncionario.setSelectedIndex(0);
                jTextField_Quantidade.setText("0");
                jFormattedTextField_DataCompra.setText(Util.getDataAtual());
                jFormattedTextField_ValorCompra.setText("00000.00");
            } else {
                objFachadaBDR.atualizarCompra(compra);
            }

            String acao = (idCompra == 0) ? "Cadastrada" : "Atualizada";

            jLabel_Mensagem.setText("Compra " + acao + " com Sucesso.");
            jLabel_Mensagem.setForeground(Color.BLACK);


        } catch (Exception ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        }

    }//GEN-LAST:event_jButton_OkActionPerformed

    private void jComboBox_idFornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_idFornecedorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox_idFornecedorActionPerformed

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:

        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");
        if (cancelar == JOptionPane.YES_OPTION) {

            dispose();

        }
    }//GEN-LAST:event_jButton_CancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadCompra(2).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JCheckBox jCheckBox_status;
    private javax.swing.JComboBox jComboBox_idFornecedor;
    private javax.swing.JComboBox jComboBox_idFuncionario;
    private javax.swing.JComboBox jComboBox_idSolicitacaoCompra;
    private javax.swing.JFormattedTextField jFormattedTextField_DataCompra;
    private javax.swing.JFormattedTextField jFormattedTextField_ValorCompra;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel_Codigo;
    private javax.swing.JLabel jLabel_Mensagem;
    private javax.swing.JLabel jLabel_Quantidade;
    private javax.swing.JLabel jLabel_idFornecedor;
    private javax.swing.JLabel jLabel_idFuncionario;
    private javax.swing.JLabel jLabel_idSolicitacaoCompra;
    private javax.swing.JLabel jLabel_valorCompra;
    private javax.swing.JTextField jTextField_Quantidade;
    private javax.swing.JTextField jTextField_idCodigo;
    // End of variables declaration//GEN-END:variables
}
